package com.zhiyou100.www.bbs.dao;

import com.zhiyou100.www.bbs.entityclass.TableUser;
import com.zhiyou100.www.bbs.entityclass.UserLoginForm;
import com.zhiyou100.www.bbs.function.jdbctool.C3P0_Util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @packageName: UserLoginFormDao
 * @className: com.zhiyou100.www.bbs.dao
 * @Description: TODO 登陆表单dao
 * @author: yang
 * @date: 2020/6/6
 */
public class UserLoginFormDao {
    /**
     * @Description: TODO 添加一个id到用户登陆表单
     * @name: addOneToTable
     * @param: [id]
     * @return: boolean
     * @date: 2020/6/6 9:24 上午
     * @author: yang
     */
    public boolean addOneToTable(Integer id) {

        Connection connection = C3P0_Util.getConnection();
        // 获取链接
        String sql = "insert into userLoginForm (userId)\n" +
                "values (?);";
        // 准备sql模版
        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement(sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        // 发送模版
        try {
            assert statement != null;
            statement.setInt(1, id);
            // 设置参数
        } catch (SQLException thenables) {
            thenables.printStackTrace();
        }
        int i = 0;
        try {
            i = statement.executeUpdate();
            // 获取影响的行数
        } catch (SQLException thenables) {
            thenables.printStackTrace();
        }
        C3P0_Util.close(connection, statement, null);
        // 关闭链接

        return i != 0;
    }

    /**
     * @Description: TODO 根据 id 获取一个 TableUser
     * @name: getOneTableUser
     * @param: [id]
     * @return: com.zhiyou100.www.bbs.entityclass.TableUser
     * @date: 2020/6/6 9:35 上午
     * @author: yang
     */
    public TableUser getOneTableUser(Integer id) {
        TableUser tableUser = new TableUser();
        // 创建一个 tableUser
        Connection connection = C3P0_Util.getConnection();
        // 获取链接

        String sql = "select * from tableUser where userId=?";
        // select * from tableUser where userId=1;
        // 准备 sql

        try {
            PreparedStatement statement = connection.prepareStatement(sql);
            // 发送模版

            statement.setInt(1, id);
            // 设置参数

            ResultSet resultSet = statement.executeQuery();
            // 获取结果集

            while (resultSet.next()) {
                //
                int userId = resultSet.getInt(1);
                String userName = resultSet.getString(2);
                String userPwd = resultSet.getString(3);
                String userRealName = resultSet.getString(4);
                String userHeadPortrait = resultSet.getString(5);
                String userGender = resultSet.getString(6);
                int userScore = resultSet.getInt(7);
                int userStatus = resultSet.getInt(8);
                // 获取参数
                tableUser = new TableUser(userId, userName, userPwd, userRealName, userHeadPortrait, userGender, userScore, userStatus);
                // 设置参数
            }
            C3P0_Util.close(connection, statement, resultSet);
            // 关闭链接

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return tableUser;
    }

    /**
     * @Description: TODO 判断当前用户登陆表 有几个用户
     * @name: determineHowManyUsersThereAreCurrently
     * @param: []
     * @return: int
     * @date: 2020/6/6 11:03 上午
     * @author: yang
     */
    public int determineHowManyUsersThereAreCurrently() {
        int cnt = 0;
        // 记录 数量
        Connection connection = C3P0_Util.getConnection();
        // 获取链接

        String sql = "select * from userLoginForm";
        // select * from tableUser where userId=1;
        // 准备 sql

        try {
            PreparedStatement statement = connection.prepareStatement(sql);
            // 发送模版

            ResultSet resultSet = statement.executeQuery();
            // 获取结果集

            while (resultSet.next()) {
                cnt++;
            }
            C3P0_Util.close(connection, statement, resultSet);
            // 关闭链接

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return cnt;
    }

    /**
     * @Description: TODO 清空 用户登陆表
     * @name: dropTheTable
     * @param: []
     * @return: void
     * @date: 2020/6/6 11:07 上午
     * @author: yang
     */
    public void dropTheTable() {
        Connection connection = C3P0_Util.getConnection();
        // 获取链接

        String sql = "delete\n" +
                "from userLoginForm;";
        // select * from tableUser where userId=1;
        // 准备 sql

        try {
            PreparedStatement statement = connection.prepareStatement(sql);
            // 发送模版

            statement.executeUpdate();
            // 执行语句

            C3P0_Util.close(connection, statement, null);
            // 关闭链接

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }


    /**
     * @Description: TODO 根据 id  删除一个
     * @name: delOne
     * @param: [id]
     * @return: void
     * @date: 2020/6/6 11:15 上午
     * @author: yang
     */
    public void delOne(Integer id) {
        Connection connection = C3P0_Util.getConnection();
        // 获取链接
        String sql = "delete from userLoginForm where userId=?";
        // delete from userLoginForm where userId=1004;
        // 准备 sql 模版
        try {
            PreparedStatement statement = connection.prepareStatement(sql);
            // 发送模版
            statement.setInt(1,id);
            // 设置参数
            statement.executeUpdate();
            // 执行
            C3P0_Util.close(connection,statement,null);
            // 关闭
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


    }


}
